const express = require('express');
const { check } = require('express-validator');
const router = express.Router();
const {
    register,
    login,
    getCurrentUser,
    updateUser,
    addAddress,
    updatePassword
} = require('../controllers/userController');
const { protect } = require('../middleware/auth');

// 注册验证规则
const registerValidation = [
    check('username', '用户名至少需要3个字符').isLength({ min: 3 }),
    check('email', '请提供有效的邮箱地址').isEmail(),
    check('password', '密码至少需要6个字符').isLength({ min: 6 })
];

// 公开路由
router.post('/register', registerValidation, register);
router.post('/login', login);

// 需要认证的路由
router.use(protect); // 应用认证中间件
router.get('/me', getCurrentUser);
router.put('/update', updateUser);
router.post('/address', addAddress);
router.put('/password', updatePassword);

module.exports = router;
